package com.ruoyi.web.controller.system;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
import com.ruoyi.system.domain.GwData;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.GwDataIService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("gwData")
public class GwDataController {


    @Resource
    private GwDataIService gwDataIService;

    @Resource
    private ISysDictDataService iSysDictDataService;

    @Resource
    private ISysDictTypeService iSysDictTypeService;




    private static Map<String,String> typeMap=new ConcurrentHashMap<>(12);

    @Anonymous
    @GetMapping("test")
    //@DataSource(DataSourceType.SLAVE)
    public void test(){

/*        List<GwData> list = gwDataIService.list();
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
        List<SysDictType> typeList = iSysDictTypeService.list();
        for (SysDictType sysDictType : typeList) {
            typeMap.put(sysDictType.getRemark(),sysDictType.getDictType());
        }
        DynamicDataSourceContextHolder.clearDataSourceType();

        dictData(list);*/

        List<SysDictType> list = iSysDictTypeService.selectDictTypeAll();

        for (SysDictType sysDictType : list) {
            typeMap.put(sysDictType.getDictType(),sysDictType.getRemark());
        }

        List<SysDictData> sysDictData = iSysDictDataService.list();

        for (SysDictData sysDictDatum : sysDictData) {
            String typeId = typeMap.get(sysDictDatum.getDictType());
            sysDictDatum.setDictTypeId(typeId);

            iSysDictDataService.saveOrUpdate(sysDictDatum);
        }


    }



    public void dictData(List<GwData> list){

        //DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
        for (GwData gwData : list) {

            SysDictData sysDictData=new SysDictData();

            sysDictData.setDictLabel(gwData.getLabel());
            sysDictData.setDictValue(gwData.getValue());
            sysDictData.setDictType(typeMap.get(gwData.getDictTypeId()));
            sysDictData.setIsDefault("Y");
            sysDictData.setStatus("0");
            sysDictData.setCreateBy("admin");
            sysDictData.setCreateTime(new Date());

            iSysDictDataService.save(sysDictData);

        }

        //DynamicDataSourceContextHolder.clearDataSourceType();
    }


}
